package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMdLirunbiao;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyMdLirunbiaoMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMdCaiwuParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMdLirunbiaoParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMdLirunbiaoResult;
import cn.stylefeng.guns.sys.modular.system.model.result.OrderDataResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyMdLirunbiaoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;

/**
 * <p>
 * 网上建店信息gcg_wsjd 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-09-08
 */
@Service
public class BtyMdLirunbiaoServiceImpl extends ServiceImpl<BtyMdLirunbiaoMapper, BtyMdLirunbiao> implements BtyMdLirunbiaoService {

    @Override
    public void add(BtyMdLirunbiaoParam param) {
        BtyMdLirunbiao entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyMdLirunbiaoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyMdLirunbiaoParam param) {
        BtyMdLirunbiao oldEntity = getOldEntity(param);
        BtyMdLirunbiao newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyMdLirunbiaoResult findBySpec(BtyMdLirunbiaoParam param) {
        return null;
    }

    @Override
    public List<BtyMdLirunbiaoResult> findListBySpec(BtyMdLirunbiaoParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyMdLirunbiaoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<OrderDataResult> findAllLirunData(BtyMdCaiwuParam param) {
        List<OrderDataResult> list = this.baseMapper.findAllLirunData(param);
        if (CollectionUtil.isNotEmpty(list)) {
            for (OrderDataResult data : list) {
                if (StringUtils.isNotBlank(data.getValue()) && data.getName().contains("%")) {
                    data.setValue(dealPercent(data.getValue()));
                } else {
                    data.setValue(dealDecimal(data.getValue()));
                }
            }
        }

        return list;
    }

    @Override
    public List<OrderDataResult> syfindAllLirunData(BtyMdCaiwuParam param) {
        List<OrderDataResult> list = this.baseMapper.syfindAllLirunData(param);
        if (CollectionUtil.isNotEmpty(list)) {
            for (OrderDataResult data : list) {
                if (StringUtils.isNotBlank(data.getValue()) && data.getName().contains("%")) {
                    data.setValue(dealPercent(data.getValue()));
                } else {
                    data.setValue(dealDecimal(data.getValue()));
                }
            }
        }

        return list;
    }

    @Override
    public List<OrderDataResult> snfindAllLirunData(BtyMdCaiwuParam param) {
        List<OrderDataResult> list = this.baseMapper.snfindAllLirunData(param);
        if (CollectionUtil.isNotEmpty(list)) {
            for (OrderDataResult data : list) {
                if (StringUtils.isNotBlank(data.getValue()) && data.getName().contains("%")) {
                    data.setValue(dealPercent(data.getValue()));
                } else {
                    data.setValue(dealDecimal(data.getValue()));
                }
            }
        }

        return list;
    }
    @Override
    public List<OrderDataResult> findAllLirunzgData(BtyMdCaiwuParam param) {
        List<OrderDataResult> list = this.baseMapper.findAllLirunzgData(param);
        if (CollectionUtil.isNotEmpty(list)) {
            for (OrderDataResult data : list) {
                if (StringUtils.isNotBlank(data.getValue()) && data.getName().contains("%")) {
                    data.setValue(dealPercent(data.getValue()));
                } else {
                    data.setValue(dealDecimal(data.getValue()));
                }
            }
        }
        return list;
    }

    @Override
    public void addList(List<BtyMdLirunbiaoParam> list) {
        baseMapper.addList(list);
    }

    private String dealDecimal(String value) {
        if (value == null || (new BigDecimal(value)).compareTo(BigDecimal.ZERO) == 0) {
            return "0.00";
        }
        return new BigDecimal(value).setScale(2, BigDecimal.ROUND_DOWN).toString();
    }

    private String dealPercent(String value) {
        if (value == null || (new BigDecimal(value)).compareTo(BigDecimal.ZERO) == 0) {
            return "0.00%";
        }
        BigDecimal bigDecimal1 = new BigDecimal(value);
        //下面将结果转化成百分比
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(2);
        return percent.format(bigDecimal1.doubleValue());
    }

    private Serializable getKey(BtyMdLirunbiaoParam param) {
        return param.getBtyMdLirunbiaoId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyMdLirunbiao getOldEntity(BtyMdLirunbiaoParam param) {
        return this.getById(getKey(param));
    }

    private BtyMdLirunbiao getEntity(BtyMdLirunbiaoParam param) {
        BtyMdLirunbiao entity = new BtyMdLirunbiao();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
